package com.baizhi.realm;

import com.baizhi.dao.CmfzAdminDao;
import com.baizhi.entity.CmfzAdmin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.beans.factory.annotation.Autowired;

public class AuthenRealm extends AuthenticatingRealm {

    @Autowired
    private CmfzAdminDao cmfzAdminDao;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        1.从令牌中获取用户名
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();

//        2.通过Dao查询数据库 获取数据库中保存的数据
        CmfzAdmin cmfzAdmin = cmfzAdminDao.selectOne(new QueryWrapper<CmfzAdmin>().eq("username", username));

//        对象不为null 封装info
        if (cmfzAdmin != null) {
            /**
             * 参数1 数据库查到的账号
             * 参数2 数据库查到的密码
             * 参数3 当前对象的名字
             */
            return new SimpleAuthenticationInfo(cmfzAdmin.getUsername(),cmfzAdmin.getPassword(),this.getName());
        }

        return null;
    }
}
